app.controller('loginPageCtrl',function($scope, AddressSevice, $cordovaCamera,PopupService,$http,$myconfig,$state,$stateParams, $ionicLoading, $timeout, $rootScope,$mydialog,$ionicPlatform,$ionicConfig, FetchDataService){
    if(localStorage.getItem('wxinfo')){
        $scope.wxinfo = JSON.parse(localStorage.getItem('wxinfo'));
    }

    //用户登录预置空对象
    $scope.loginMes = {};

    //用户登录
    $scope.loginin = function(){
        if(!$scope.loginMes.mobile){
            $scope.err = '请输入手机号';
            $mydialog.fail($scope);
            return;
        }else if(!$scope.loginMes.password){
            $scope.err = '请输入正确密码';
            $mydialog.fail($scope);
            return;
        }
       if($scope.wxinfo){
            $http.post($myconfig.api.base_host + '/users/update_wx' ,{
                "mobile":$scope.loginMes.mobile,
                "password":$scope.loginMes.password,
                "wxinfo" : $scope.wxinfo
            }).then(function(res){
                // console.log(res.data);
                // return;
                    if(res.data.success){
                        localStorage.setItem('token',res.data.info.access_token);
                        $scope.success = true;
                        localStorage.setItem('user',JSON.stringify(res.data.info.user));
                        $rootScope._RootUser = res.data.info.user;
                        $http.post($myconfig.api.base_host + '/users/update_one_user',{
                            mobile: $scope.loginMes.mobile,
                            headimgurl: $scope.wxinfo.headimgurl,
                            // username: res.data.info.user.username
                        }).then(function(res){
                            if(res.data.success){
                                function fn(val){
                                    // console.log(val);
                                    $http.post($myconfig.api.base_host + '/users/update_point',{
                                        "point":val
                                    }).then(function(res){
                                        console.log(res);
                                    });
                                }
                                AddressSevice.getPosition(fn);
                                $state.go('app.home');
                            }else{
                                $scope.err = res.data.data;
                                $mydialog.fail($scope);
                            }
                        });

                        FetchDataService.initData();

                    }else{
                        $scope.err = res.data.data;
                        $mydialog.fail($scope);
                    }
                });
       }else{

            $http.post($myconfig.api.base_host + '/users/log_in' ,{
                "mobile":$scope.loginMes.mobile,
                "password":$scope.loginMes.password
            }).then(function(res){
                // console.log(res.data);
                // return;
                if(res.data.success){
                    localStorage.setItem('token',res.data.info.access_token);
                    localStorage.setItem('refresh_token',res.data.info.refresh_token);
                    $scope.success = true;
                    localStorage.setItem('user',JSON.stringify(res.data.info.user));
                    $rootScope._RootUser = res.data.info.user;
                    function fn(val){
                        // console.log(val);
                        $http.post($myconfig.api.base_host + '/users/update_point',{
                            "point":val
                        }).then(function(res){
                            console.log(res);
                        });
                    }
                    AddressSevice.getPosition(fn);
                    $state.go('app.home');

                    FetchDataService.initData();
                }else{
                    $scope.err = res.data.data;
                     $mydialog.fail($scope);
                }
            });
        }
    };


    //忘记密码
    $scope.findPwd = function(num){
        if(!num){
             $state.go('app.findpwd');
        }else if(num){
            $state.go('app.findpwd',{'phoNum':num.toString()});
        }
    };

    //微信登录
    //微信登录服务器返回参数之前不得二次点击
    var isWXOK = true;
    $scope.wxLogin = function(){
        //微信验证
        if(isWXOK){
            isWXOK = !isWXOK;
            var scope = "snsapi_userinfo",
                state = "_" + (+new Date());
            Wechat.auth(scope, state, function (response) {
                // you may use response.code to get the access token.
                // alert(JSON.stringify(response));
                // console.log(response);
                $http.get($myconfig.api.base_host  + '/openWX/getUserInfo?code=' + response.code)
                .then(function(res){
                    // alert(res);
                    // console.log(res);
                    isWXOK = true;
                    if(res.data.signin){
                        $scope.success = true;
                        localStorage.setItem('token',res.data.info.access_token);
                        localStorage.setItem('user',JSON.stringify(res.data.data));
                        $rootScope._RootUser = res.data.data;
                        $state.go('app.home');
                    }else if(!res.data.signin){
                        localStorage.setItem('wxinfo',JSON.stringify(res.data.data));
                        $state.go('app.loginWX');
                    }
                });
            }, function (reason) {
                 console.log("Failed: " + reason);
            });
        }else if(!isWXOK){
            console.log('服务器未返回数据之前不得二次点击执行函数！！！');
        }
    };




    //清空输入框内容
    $scope.clearMes = function(i){
        switch (i){
            case 0:
                $scope.loginMes.mobile = '';
                break;
            case 1:
                $scope.loginMes.password = '';
                break;
        }
    };


    $scope.GoRegis = function(){
        $state.go('app.register');
    };

      $scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, options) {
        $ionicConfig.views.swipeBackEnabled(false);
        if (toState.name == 'app.vip_message_newpwd') {
            event.preventDefault();
            toState.name == 'app.home';
        }
    });

});
